﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using KQXS.Utils;
using System.Threading;
using KQXS.Reports;

namespace KQXS.View
{
    public partial class frmHoaHongTheCanBo : DevExpress.XtraEditors.XtraForm
    {
        public frmHoaHongTheCanBo()
        {
            InitializeComponent();
        }

        private void frmHoaHongTheCanBo_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            marqueeProgressBarControl1.Properties.Stopped = true;            
            if (validate())
            {
                deDenngay.DateTime = CommonFunctions.maxNgay(DateTime.Now.Year, DateTime.Now.Month);
                deTungay.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);                
            }
            loadcanbo();            
        }

        public bool validate()
        {
            bool flag = true;
            DateTime tu = deTungay.DateTime;
            DateTime den = deDenngay.DateTime;

            if (tu.CompareTo(den) > 0)
            {
                XtraMessageBox.Show("Có lỗi !");
                flag = false;
            }
            return flag;
        }


        SqlConnection conn = new SqlConnection(BienTc.connstring);
        private void loadcanbo()
        {
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlDataAdapter da = new SqlDataAdapter("Select Ma_DT +' - '+Ten_DT as Ten, * from DMCanBo Where isPublic=1", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);

                DataRow row = dt.NewRow();
                row["Ma_DT"] = "TC";
                row["Ten"] = "Công ty";
                row["Ten_DT"] = "Công ty";
                dt.Rows.InsertAt(row, 0);
                lueCanBo.Properties.DataSource = dt;
                if (dt.Rows.Count > 0)
                {
                    lueCanBo.ItemIndex = 0;
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }

        //private void LoadThangNam()
        //{
        //    for (int i = 1; i < 13; i++)
        //    {
        //        cbbThang.Items.Add("Tháng " + i);
        //    }
        //    cbbThang.SelectedIndex = DateTime.Now.Month - 1;

        //    int Nam = DateTime.Now.Year;

        //    for (int i = Nam; i > Nam - 10; i--)
        //    {
        //        cbbNam.Items.Add("Năm " + i);
        //    }
        //    cbbNam.SelectedIndex = 0;
        //}

        private void btnIn_Click(object sender, EventArgs e)
        {
            marqueeProgressBarControl1.Properties.Stopped = false;
            Thread t = new Thread(new ThreadStart(inbaocao));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void inbaocao()
        {
            //int thang = cbbThang.SelectedIndex + 1;
            //string n = cbbNam.Text;
            //string n2 = "";
            //if (n.Contains("Năm"))
            //{
            //    n2 = n.Replace("Năm", "");
            //}
            //int nam = int.Parse(n2.ToString().Trim());
            DateTime tuNgay = DateTime.Now;
            DateTime denNgay = DateTime.Now;
            tuNgay = deTungay.DateTime;
            denNgay = deDenngay.DateTime;
            string macanbo = lueCanBo.GetColumnValue("Ma_DT").ToString();
            string tencanbo = lueCanBo.GetColumnValue("Ten_DT").ToString();
            rptDTHHDaiLy rpt = new rptDTHHDaiLy(macanbo, tuNgay, denNgay,tencanbo);
            rpt.ShowRibbonPreviewDialog();
            marqueeProgressBarControl1.Properties.Stopped = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();                
        }
    }
}